<?php
namespace app\admin\controller;
use think\Session;
use think\Cookie;
use think\Loader;
use app\model\AdminUsers;
use app\common\controller\BaseController;
class LoginController extends BaseController
{

	public function login(){
    	if(check_admin()){
    		$this->redirect('admin/Index/index');
    	}
    	if($this->request->isPost()){
    		$data = [
    			'account' => $this->request->post('account'),
    			'password' => $this->request->post('password'),
    		];

            $validate = Loader::validate('User');

    		if(!$validate->check($data)){
                return msgReturn(false,$validate->getError(),null);
    		}

            $result = AdminUsers::get($data);

            if($result){
                if ($result['status'] != 1) {
                    return msgReturn(false,'已经被禁用',null);
                } else {
                    Session::set('aid',$result['id']);
                    Session::set('admin_type',$result['type']);
                    Cookie::set('username',$result['account']);
                    return msgReturn();
                }
            }
            else{
                return msgReturn(false,'用户名密码错误',null);
            }
    	}
    	else{
    		return $this->fetch();
    	}
    }

    public function logout(){
        Session::delete('aid');
        Session::delete('admin_type');
        $this->redirect('admin/Login/login');
    }
}